package com.travel.server.manager.impl.command.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.data.QueryOperateRefundAnalysisRequest;
import com.travel.server.manager.dto.response.data.QueryOperateRefundAnalysisResponse;
import com.travel.server.manager.dto.response.data.QueryOperateRefundAnalysisResponse.RefundItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.RefundDal;
import com.travel.server.manager.impl.sal.OrderDataSal;
import com.travel.server.manager.impl.vo.DataDateVo;
import com.travel.server.manager.model.dto.data.RefundAnalysisDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/14 19:13
 * @Desc:
 */
@Component
@Slf4j
public class QueryOperateRefundAnalysisCommand extends AbstractCommand<QueryOperateRefundAnalysisRequest, QueryOperateRefundAnalysisResponse> {

    @Autowired
    private RefundDal refundDal;

    private final String numKey = "_num";

    private final String amountKey = "_amount";

    private final String totalKey = "_total";

    @Override
    protected void checkInput(QueryOperateRefundAnalysisRequest request) {
        checkTimeInput(request);
        if(request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryOperateRefundAnalysisResponse handle(QueryOperateRefundAnalysisRequest request) {
        checkInput(request);
        QueryOperateRefundAnalysisResponse response = new QueryOperateRefundAnalysisResponse();
        DataDateVo dateVo = OrderDataSal.getDate(request.getType(), request);
        Date startTime = dateVo.getStartTime();
        Date endTime = dateVo.getEndTime();
        Date lastTime = dateVo.getLastTime();
        List<RefundAnalysisDto> trendList = refundDal.selectRefundAnalysis(lastTime, endTime);

        Map<String, Integer> map = makeMap(trendList);
        List<RefundItem> refundNumList = new ArrayList<>();
        List<RefundItem> refundAmountList = new ArrayList<>();
        List<RefundItem> refundRatioList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        while(calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)){
            RefundItem numItem = new RefundItem();
            RefundItem amountItem = new RefundItem();
            RefundItem ratioItem = new RefundItem();
            String day = DateUtil.YYYY_MM_DD.format(calendar.getTime());
            numItem.setDay(calendar.getTime());
            Integer order = map.get(day + numKey);
            order = order == null ? 0 : order;
            Integer amount = map.get(day + amountKey);
            Integer total = map.get(day + totalKey);
            total = total == null ? 0 : total;
            numItem.setDay(calendar.getTime());
            amountItem.setDay(calendar.getTime());
            ratioItem.setDay(calendar.getTime());
            numItem.setOrder(order + "");
            amountItem.setAmount(amount == null ? "0" : amount + "");
            ratioItem.setRatio(CommonUtil.getProportion(order, total));

            refundNumList.add(numItem);
            refundAmountList.add(amountItem);
            refundRatioList.add(ratioItem);

            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        int currentRefundAmount = 0;
        int currentRefundNum = 0;
        int currentTotal = 0;
        int lastRefundAmount = 0;
        int lastRefundNum = 0;
        int lastTotal = 0;
        if(!CollectionUtil.isEmpty(trendList)) {
            for (RefundAnalysisDto dto : trendList) {
                Date date = DateUtil.getDataTime(dto.getDays());
                if (date.before(startTime)) {
                    lastRefundAmount += dto.getRefundAmount();
                    lastRefundNum += dto.getRefundNum();
                    lastTotal += dto.getTotal();
                } else {
                    currentRefundAmount += dto.getRefundAmount();
                    currentRefundNum += dto.getRefundNum();
                    currentTotal += dto.getTotal();
                }
            }
        }


        response.setTodayRefundAmount(CommonUtil.fenToYuan(currentRefundAmount).toString());
        response.setTodayRefundOrder(currentRefundNum + "");
        response.setTodayRefundRatio(CommonUtil.getProportion(currentRefundNum, currentTotal));
        response.setYesterdayRefundAmount(CommonUtil.fenToYuan(lastRefundAmount).toString());
        response.setYesterdayRefundOrder(lastRefundNum + "");
        response.setYesterdayRefundRatio(CommonUtil.getProportion(lastRefundNum, lastTotal));


        response.setRefundAmountList(refundAmountList);
        response.setRefundOrderList(refundNumList);
        response.setRefundRatioList(refundRatioList);

        return response;
    }

    private Map<String, Integer> makeMap(List<RefundAnalysisDto> list){
        Map<String, Integer> map = new HashMap<>();
        if(!CollectionUtil.isEmpty(list)){
            for(RefundAnalysisDto dto : list){
                String day = dto.getDays();
                map.put(day + numKey, dto.getRefundNum());
                map.put(day + amountKey, dto.getRefundAmount());
                map.put(day + totalKey, dto.getTotal());
            }
        }
        return map;
    }
}
